package cn.rayyee.base{
	import flash.display.Sprite;
	import flash.text.engine.*;
	public class BaseText extends Sprite
	{
		private var _wordWrap:Boolean;
		private var _textElement:TextElement;
		private var _fontName:String;
		private var _fontSize:int;
		private var _color:uint;
		public function BaseText() {
			_fontName = "Comfortaa";
			_fontSize = 16;
			_color = 0x000000;
			_textElement = new TextElement();
		}
		
		private function initText():void 
		{
			if(numChildren>0)	removeChildAt(0);
			var fd:FontDescription = new FontDescription(_fontName);
			var ef:ElementFormat = new ElementFormat(fd);
			ef.fontSize = _fontSize;
			ef.color = _color;
			_textElement.elementFormat = ef;
			var tb:TextBlock = new TextBlock();
			tb.content = _textElement;
			createLines(tb);
		}
		
		private function createLines(textBlock:TextBlock):void
		{
			var lineWidth:Number = 500;
			var xPos:Number = 15.0;
			var yPos:Number = 20.0;
			var textLine:TextLine;
			
			if (!_wordWrap)  textLine = textBlock.createTextLine(null);
			else textLine = textBlock.createTextLine(null,lineWidth);
			
			while (textLine)
			{
				textLine.x = xPos;
				textLine.y = yPos;
				yPos +=  textLine.height + 2;
				addChild(textLine);
				textLine = textBlock.createTextLine(textLine,lineWidth);
			}
		}
		
		/// A Boolean value that indicates whether the text field has word wrap.
		public function get wordWrap () : Boolean { return _wordWrap; }
		public function set wordWrap (value:Boolean) : void
		{
			_wordWrap = value;
			
		}
		
		public function get text():String { return _textElement.text; }		
		public function set text(value:String):void 
		{
			_textElement.text = value;
			initText();
		}
		
		public function get font():String { return _fontName; }
		public function set font(value:String):void 
		{
			_fontName = value;
			initText();
		}
		
		public function get fontSize():int { return _fontSize; }
		public function set fontSize(value:int):void 
		{
			_fontSize = value;
			initText();
		}
		
		public function get color():uint { return _color; }
		public function set color(value:uint):void 
		{
			_color = value;
			initText();
		}
	}
}